%% -------------------------------------------------------------------
%%
%% Copyright (c) 2019 Carlos Gonzalez Florido.  All Rights Reserved.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License.  You may obtain
%% a copy of the License at
%%
%%   http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied.  See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%% -------------------------------------------------------------------

%% @doc NkLIB OTP Application Module
-module(nklib_app).
-author('Carlos Gonzalez <carlosj.gf@gmail.com>').
-behaviour(application).

-export([start/0, start/2, stop/1]).

-define(APP, nklib).

%% ===================================================================
%% Private
%% ===================================================================



%% @doc Starts NkLIB stand alone.
-spec start() -> 
    ok | {error, Reason::term()}.

start() ->
    case nklib_util:ensure_all_started(?APP, permanent) of
        {ok, _Started} ->
            ok;
        Error ->
            Error
    end.

%% @private OTP standard start callback
start(_Type, _Args) ->
    HwAddr = nklib_util:get_hwaddr(),
    application:set_env(?APP, hw_addr, HwAddr),
    nklib_date:store_timezones(),
    nklib_i18n:store_locales([en]),
    {ok, Pid} = nklib_sup:start_link(),
    JsonEncoder = case application:get_env(?APP, json_encoder) of
        {ok, jiffy} -> jiffy;
        {ok, jsone} -> jsone;
        undefined -> jsone
    end,
    nklib_util:do_config_put(nklib_json_encoder, JsonEncoder),
    {ok, Pid}.


%% @private OTP standard stop callback
stop(_) ->
    ok.


